package com.gouwudai.gobbs.service.impl;

import java.util.List;

import org.extremecomponents.table.limit.Limit;

import com.gouwudai.gobbs.common.Constants;
import com.gouwudai.gobbs.common.IPageList;
import com.gouwudai.gobbs.dao.IClassPermissionDAO;
import com.gouwudai.gobbs.dao.IUserDAO;
import com.gouwudai.gobbs.pojo.TbbsClassPermission;
import com.gouwudai.gobbs.service.IClassPermissionService;
import com.litt.core.common.Utility;
import com.litt.core.util.ExtremeTableUtil;

/**
 * 
 * <b>标题:</b> 分类版面权限服务.
 * <pre><b>描述:</b> 
 *   管理分类版面权限
 * </pre>
 * 
 * @author <a href="mailto:littcai@hotmail.com">空心大白菜</a>
 * @since 2007-7-2
 * @version 1.0
 *
 */
public class ClassPermissionServiceImpl implements IClassPermissionService
{
	IClassPermissionDAO classPermissionDAO;
	IUserDAO userDAO;

    /**
     * 更新用户版面权限.
     * @param classId
     * @param userId
     * @param permissionCodes
     */
	public void updateUser(Integer classId,Integer userId,String permissionCodes)
	{	
        classPermissionDAO.deleteByClassUser(classId, userId);
        String[] codes = Utility.spitString(permissionCodes, ",");
        TbbsClassPermission tbbsClassPermission = new TbbsClassPermission();
        for(int i=0;i<codes.length;i++)
        {
            tbbsClassPermission = new TbbsClassPermission();
            tbbsClassPermission.setClassId(classId);
            tbbsClassPermission.setUserId(userId);
            tbbsClassPermission.setType(new Integer(0));
            tbbsClassPermission.setPermissionCode(Integer.valueOf(codes[i]));
            classPermissionDAO.save(tbbsClassPermission);
        }
        
	}

    /**
     * 读取单条权限信息.
     * 
     * @param id 主键
     * 
     * @return TbbsClassPermission
     */
	public TbbsClassPermission load(Integer id)
	{
		return (TbbsClassPermission)classPermissionDAO.load(id);
	}
	
	/**
	 * 根据用户登陆ID查询
	 * @param classId
	 * @param loginId
	 * @return
	 */
    public List listByLoginId(Integer classId,String loginId)
    {	
    	return classPermissionDAO.listByLoginId(classId, loginId);
    }
    
	/**
	 * 根据用户ID查询
	 * @param classId
	 * @param userId
	 * @return
	 */
    public List listByUserId(Integer classId,Integer userId)
    {
    	return classPermissionDAO.listByUserId(classId, userId);
    }  
    
    /**
     * 按用户ID和版面ID查询未拥有的权限
     * @param classId 分类版面ID
     * @param userId 用户ID
     * @return
     */
    public List listNotByUserId(Integer classId,Integer userId)
    {
        return classPermissionDAO.listNotByUserId(classId, userId);
    }
    
	/**
	 * 根据等级ID查询
	 * @param classId
	 * @param gradeId
	 * @return
	 */
    public List listByGradeId(Integer classId,String gradeId)
    {
    	return classPermissionDAO.listByGradeId(classId, gradeId);
    }
	
	public void delete(Integer ppId)
	{		
		classPermissionDAO.delete(ppId);
	}
   
	
	public void AutoBindingTable(javax.servlet.http.HttpServletRequest request)
	{
		Limit limit = ExtremeTableUtil.getLimit(request, Constants.DEFAULT_PAGE_SIZE);
		IPageList page = classPermissionDAO.listPage(limit.getPage(), limit.getCurrentRowsDisplayed(),ExtremeTableUtil.getSort(limit));
		request.setAttribute("resultList",page.getRsList());
		request.setAttribute("totalRows",page.getTotalSize());
	}

	public void setClassPermissionDAO(IClassPermissionDAO classPermissionDAO) {
		this.classPermissionDAO = classPermissionDAO;
	}

	public void setUserDAO(IUserDAO userDAO) {
		this.userDAO = userDAO;
	}
	
}